package com.hedl.controller;

import com.hedl.entity.User;
import com.hedl.resp.BaseResponse;
import com.hedl.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("")
    public BaseResponse findAll(){
        try {
            List<User> all = userService.findAll();
            return BaseResponse.getInstance().success(all);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询用户数据异常：{}", e.getMessage());
        }
        return BaseResponse.getInstance().error(BaseResponse.SYS_ERR_CODE);
    }

    @GetMapping("/{id}")
    public BaseResponse findById(@PathVariable("id") Long id){
        if (id == null){
            return BaseResponse.getInstance().error(BaseResponse.PARAM_ERR_CODE);
        }
        try {
            User user = userService.selectById(id);
            return BaseResponse.getInstance().success(user);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询用户数据异常：{}", e.getMessage());
        }
        return BaseResponse.getInstance().error(BaseResponse.SYS_ERR_CODE);
    }
}
